package com.solcoo.merchant.ui.menu;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.solcoo.merchant.R;
import com.solcoo.merchant.base.BaseListAdapter;
import com.solcoo.merchant.model.Menu;
import com.solcoo.merchant.util.ViewUtil;

public class MenuAdapter extends BaseListAdapter<Menu> {

	private static final int TAG_KEY = "pageClass".hashCode();

	public MenuAdapter(Context context, List<Menu> mdata) {
		super(context, mdata);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView != null) {
			holder = (ViewHolder) convertView.getTag(R.layout.menu_item);
		} else {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.menu_item, null);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.name.setOnClickListener(onClickListener);
			convertView.setTag(R.layout.menu_item, holder);
		}
		Menu m = getItem(position);
		holder.name.setText(m.name);
		holder.name.setTag(TAG_KEY, m.pageClass);
		holder.name.setCompoundDrawablesWithIntrinsicBounds(0, m.icon, 0, 0);
		return convertView;
	}

	private static class ViewHolder {
		public TextView name;
	}

	private OnClickListener onClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent it = new Intent(v.getContext(), (Class<?>) v.getTag(TAG_KEY));
			v.getContext().startActivity(it);
			ViewUtil.showToast(v.getContext(), ((TextView) v).getText()
					.toString());
		}
	};

}
